import requestApi from '../requestApi'
import presetVar from '../presetVar';
import { toast } from 'nc-lightapp-front';


export default function searchClick(_props, searchVal, isDefaultSearch) {
    this.props.transferTable.clearAllSelectedData()//先清掉已选的
    const props = this.props
    const multiLang = props.MutiInit.getIntl(2011);
    const searchValue = this.props.search.getQueryInfo('20110MTAMN', false);
   
    const { pk_jkbx, tradetype, djlxbm } = window.presetVar.reloadHead.rows[0].values
    if (isDefaultSearch === 'defaultSearch') {
        const und = typeof window.presetVar.noAmountSq === 'undefined'
        if (!searchValue.querycondition.conditions[1]) {
            searchValue.querycondition.conditions[1] = {
                "field": "noamount",
                "value": {
                    "firstvalue": false,
                    "secondvalue": ""
                },
                "oprtype": "=",
                "display": "否",
                "isIncludeSub": false,
                "originOptSign": "=",
                "refurl": "",
                "datatype": "32",
                "timeFunction": "",
                "def5": "无金额申请"
            }
        }

        if (!und) {
            searchValue.querycondition.conditions[1].value.firstvalue = window.presetVar.noAmountSq
        }
    }
    
    
    const _pageInfo = props.transferTable.getTablePageInfo(presetVar.areaCode.billsArea,presetVar.areaCode.bodysArea)
    
    let data = {
        querycondition: searchValue.querycondition,
        pagecode: presetVar.pageCode.searchPage,
        pageInfo: {
            pageIndex: _pageInfo.pageIndex,
            pageSize: isDefaultSearch?10:_pageInfo.pageSize,
        },
        pagestatus:1,
        queryAreaCode: presetVar.areaCode.searchArea, //查询区编码
        // oid:'0001Z310000000005J2S',  //查询模板id， oid从木板上取，取默认模板会导致新增的查询条件不生效
        oid: props.meta.getMeta()[presetVar.areaCode.searchArea]['oid'],
        querytype: 'tree',
        tradetype: props.getUrlParam('tradetype') || window.presetVar.tradetype
    };
    //回显已经选中的项目添加的参数

    if (pk_jkbx) {
        data.pulldata = { "pk_jkbx": pk_jkbx, pk_tradetype: tradetype ? tradetype : djlxbm }
    }
    let senddata = { data, "pagecode": presetVar.pagecode2, "tepmid": props.meta.getMeta().pageid };
    requestApi.query({
        data: senddata,
        success: (res) => {
            //head.body
            const {data,pageInfo} = res

            //是否是无金额申请
            const noAmount = props.search.getSearchValByField(presetVar.areaCode.searchArea, 'noamount')
            const isNoAmount = noAmount.value && noAmount.value.firstvalue


            //无金额 隐藏金额字段  orig_amount => head.items[] and mtapp_detail.items[]
            const metas = props.meta.getMeta()
            const filterKeys = ['orig_amount', 'pk_currtype', 'rest_amount', 'share_ratio']
            metas.head.items = isNoAmount ?
                metas.head.items.filter(item => { if (filterKeys.includes(item.attrcode)) { item.visible = false } return true })
                :
                metas.head.items.filter(item => { if (filterKeys.includes(item.attrcode)) { item.visible = true } return true })
            metas.mtapp_detail.items = isNoAmount ?
                metas.mtapp_detail.items.filter(item => { if (filterKeys.includes(item.attrcode)) { item.visible = false } return true })
                :
                metas.mtapp_detail.items.filter(item => { if (filterKeys.includes(item.attrcode)) { item.visible = true } return true })
            props.meta.setMeta(metas)


            
            if (data&&data.length) {
                //暂时不加计算表体
                // if (window.presetVar.applicationForm_table) {
                //     const { body: selectedItems } = window.presetVar.applicationForm_table.head
                //     //合并表头相同的数据
                //     let head_arr = []
                //     window.presetVar.arap_bxbills.forEach(item => {
                //         let index = head_arr.findIndex(item1 => item1.pk === item.pk_item)
                //         if (index > -1) {
                //             head_arr[index].ybje = (head_arr[index].ybje * 1000 + item.ybje * 1000) / 1000
                //         } else {
                //             head_arr.push({
                //                 pk: item.pk_item,
                //                 ybje: item.ybje
                //             })
                //         }
                //     })
                   
                //     data.forEach(item => {
                //         //[0].head.head.rows[0].values.pk_mtapp_bill.value
                //         const rowId = item.head.head.rows[0].values.pk_mtapp_bill.value
    
                //         if (selectedItems.hasOwnProperty(rowId)) {
                //             //计算表头 [0].head.head.rows[0].values.rest_amount  
    
                //             let current = head_arr.find(item2 => item2.pk === rowId)
                //             let display_calc = (item.head.head.rows[0].values.rest_amount.display * 1000 - current.ybje * 1000) / 1000,
                //                 value_calc = (item.head.head.rows[0].values.rest_amount.value * 1000 - current.ybje * 1000) / 1000;
                //             item.head.head.rows[0].values.rest_amount = {
                //                 "display": display_calc === 0 ? '0.000' : display_calc,
                //                 "scale": "3",
                //                 "value": value_calc === 0 ? '0.000' : value_calc,
                //             }
    
                //             //计算表体
                //             item.body.mtapp_detail.rows.forEach(item1 => {
                //                 //[0].body.mtapp_detail.rows[0].values.pk_mtapp_detail.value
                //                 const childId = item1.values.pk_mtapp_detail.value
                //                 if (selectedItems[rowId].includes(childId)) {
                //                     let current = window.presetVar.arap_bxbills.find(item2 => item2.pk_mtapp_detail === childId)
                //                     let detail_display_calc = (item1.values.rest_amount.display * 1000 - current.ybje * 1000) / 1000,
                //                         detail_value_calc = (item1.values.rest_amount.value * 1000 - current.ybje * 1000) / 1000
                //                     item1.values.rest_amount = {
                //                         "display": detail_display_calc === 0 ? '0.000' : detail_display_calc,
                //                         "scale": "3",
                //                         "value": detail_value_calc === 0 ? '0.000' : detail_value_calc,
                //                     }
                //                 }
                //             })
                //         }
    
                //     })
                // }




                const setData = {
                    rows: data,
                    pageInfo
                };
                if (pageInfo.pageIndex == 0) {
                    setData.allpks = new Array(Number(pageInfo.total));
                }
                
                this.props.transferTable.setTransferTableValue(presetVar.areaCode.billsArea, presetVar.areaCode.bodysArea, setData, 'pk_mtapp_bill', 'pk_mtapp_detail');
                //设置之前的勾选选中
                if (window.presetVar.applicationForm_table) {
                    const selected_ids = window.presetVar.applicationForm_table
                    if (!selected_ids) {
                        return
                    }
                    const selectedPk = selected_ids.head.body
                    let pkArr = []
                    for (const i in selectedPk) {
                        if (Array.isArray(selectedPk[i])) {
                            pkArr.push(...selectedPk[i])
                        }
                    }
                    console.log('查询===', selected_ids, selectedPk, pkArr)
                    
                    this.props.transferTable.setTheCheckRows(presetVar.areaCode.billsArea, pkArr)
                }
            }else{
                this.props.transferTable.setTransferTableValue(presetVar.areaCode.billsArea, presetVar.areaCode.bodysArea, [], 'pk_mtapp_bill', 'pk_mtapp_detail');
               
            } 
             toast({
                color: 'success',
                title: multiLang && multiLang.get('201102BCLF-0055'),
                content: (multiLang && multiLang.get('201102BCLF-0056') || '').replace('{count}', pageInfo && pageInfo.total || 0)
            })

            


        }
    })

}

